package com.sakura.mall.product.productAttrValue.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.product.productAttrValue.domain.entity.ProductAttrValueEntity;
import com.sakura.mall.product.productAttrValue.domain.request.AddProductAttrValueRequest;
import com.sakura.mall.product.productAttrValue.domain.request.UpdateProductAttrValueRequest;
import com.sakura.mall.product.productAttrValue.domain.request.ProductAttrValuePageRequest;
import com.sakura.mall.product.productAttrValue.domain.vo.ProductAttrValueVo;
import com.sakura.mall.product.productAttrValue.mapper.ProductAttrValueMapper;
import com.sakura.mall.product.productAttrValue.service.ProductAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * spu属性值 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueMapper, ProductAttrValueEntity> implements ProductAttrValueService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddProductAttrValueRequest request) {
        // todo 其他校验逻辑
        ProductAttrValueEntity productAttrValue = request.convert(ProductAttrValueEntity.class);
        this.save(productAttrValue);
    }

    @Override
    public Page<ProductAttrValueVo> queryPage(ProductAttrValuePageRequest request) {
        IPage<ProductAttrValueEntity> qp = request.toPage();
        LambdaQueryWrapper<ProductAttrValueEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<ProductAttrValueVo> ret = qp.convert(entity -> entity.convert(ProductAttrValueVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateProductAttrValueRequest request) {
        ProductAttrValueEntity entity = request.convert(ProductAttrValueEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

